package com.xintonghua.bussiness.ui.user;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.view.Window;
import android.widget.ImageView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.MyUtils;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.ShareAdapter;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.Auth;
import com.xintonghua.bussiness.bean.MyShareBean;
import com.xintonghua.bussiness.util.MyState;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wanxianyi
 * 时间：2018/3/2.
 * 包名：com.xintonghua.bussiness.ui.user
 */

public class MyShareActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tab_share)
    TabLayout tabShare;
    @BindView(R.id.lv_share)
    XRecyclerView lvShare;
    String[] title = new String[]{"I级客户", "II级客户"};
    ShareAdapter adapter;
    private List<MyShareBean.FirstBean> firstList;
    private List<MyShareBean.FirstBean> sencondList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_my);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        for (String str : title) {
            tabShare.addTab(tabShare.newTab().setText(str));
        }
        httpCent.mySharedList(this, MyState.requestOne);
        tabShare.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getPosition() == 0) {
                    adapter = new ShareAdapter(MyShareActivity.this, firstList);
                } else {
                    adapter = new ShareAdapter(MyShareActivity.this, sencondList);
                }
                lvShare.setAdapter(adapter);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        RefreshUtils.initList(this, lvShare, MyUtils.dip2px(this, 4), R.color.grey_bg);
        // lvShare.setLoadingListener(this);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                MyShareBean bean = JSONObject.parseObject((String) data, MyShareBean.class);
                firstList = bean.getFirst();
                sencondList = bean.getSecond();
                adapter = new ShareAdapter(this, firstList);
                lvShare.setAdapter(adapter);
                lvShare.refreshComplete();
                break;
        }
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        httpCent.mySharedList(this, MyState.requestOne);
    }

    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
